<?php

/**
 * 单独问卷excel文件处理
 * User: Pulingao
 * Date: 2014/11/30
 */
class QuestionExcel extends Excel {

	/**
	 * 导出问卷基本数据
	 * @param type $tableData	问卷表基本数据
	 * @param type $fileName	导出文件名称
	 * @param type $type		导出文件类型，xls or xlsx
	 * @return type
	 */
	public function questionExport($tableData, $fileName = 'question_export', $type = 'xls') {
		if (empty($tableData)) {
			return array();
		}
		
		$fillData = array();
		foreach($tableData as $key => $data){
			$fillData[$key][] = $data['identifier'];
			$fillData[$key][] = $data['question_name'];
			$fillData[$key][] = '--';
			$fillData[$key][] = $data['project_id'];
			$fillData[$key][] = $data['status'];
			$fillData[$key][] = date('Y-m-d H:i:s', $data['create_time']);
		}
		# 设置导出基本信息
		$this->FileName = $fileName;
		$this->Type = $type;
		$this->exportHeader();

		# 设置excel头部内容
		$excelTitles = array(
			'问卷编号', '问卷名称', '创始人', '所属项目', '状态', '创建时间'
		);
		foreach ($excelTitles as $k => $v) {
			$this->PHPExcelExport->setActiveSheetIndex(0)->setCellValueByColumnAndRow($k,1, $v);
			$this->PHPExcelExport->getActiveSheet()->getStyleByColumnAndRow($k,1)
					->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFC000');
			$this->PHPExcelExport->getActiveSheet()->getStyleByColumnAndRow($k,1)
					->getFont()->setSize(10)->setBold(true);
		}

		$x = 2;
		foreach ($fillData as $value) {
			$y = 0;
			foreach ($value as $k => $v) {
				$this->PHPExcelExport->setActiveSheetIndex(0)->setCellValueByColumnAndRow($y, $x, $v);
				// 让对应的excel表格自动换行
				$this->PHPExcelExport->getActiveSheet()->getStyleByColumnAndRow($y, $x)->getAlignment()->setWrapText(true);
				$this->PHPExcelExport->getActiveSheet()->getStyleByColumnAndRow($y, $x)->getFont()->setSize(10);
				// 垂直居中
				$this->PHPExcelExport->getActiveSheet()->getStyleByColumnAndRow($y,$x)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				$this->PHPExcelExport->getActiveSheet()->getRowDimension($x)->setRowHeight(15);
				$this->PHPExcelExport->getActiveSheet()->getColumnDimensionByColumn($y)->setWidth(30);
				$y++;
			}
			$x++;
		}
		
		$this->exportFooter();
	}
	
	/**
	 * 处理添加问卷时网点excel文件导入
	 * @param type $file	文件名
	 * @param type $type	文件类型 xls or xlsx
	 */
	public function questionNetworkImport($file, $type = 'xlsx'){
		if(!file_exists($file)){
			throw new Exception('文件未找到');
		}
		$this->Type = $type;
		$this->importBase($file);
		$msg['cols'] = $this->ColLength;
		$msg['rows'] = $this->RowLength;
		
		// 中间处理导入逻辑，循环解析excel
		for($rowNum=1;$rowNum<=$msg['rows'];$rowNum++){
			for($colNum=0;$colNum<$msg['cols'];$colNum++){
				$item = $this->PHPExcelImport->getCellByColumnAndRow($colNum,$rowNum);
				$realRow = self::excelValue($item);

				$tmpData = array(
					'title' => $realRow,
					'coordinate' => $item->getCoordinate()
				);
				switch ($rowNum) {
					case 1:	#单个sheet表头信息
						$excelData['data_th'][$rowNum][$colNum] = $tmpData;
						break;
					default:
						$excelData['data'][$rowNum][$colNum] = $tmpData;
						break;
				}
			}
		}
		
		return array(
			'status' => true,
			'msg' => $excelData
		);
		
	}
	
	/**
	 * 解析问卷内容（问卷导入）
	 * @param type $file
	 * @param type $type
	 * @throws Exception
	 */
	public function questionContentImport($file, $type = 'xlsx'){
		if(!file_exists($file)){
			throw new Exception('文件未找到');
		}
		$this->Type = $type;
		$this->importBase($file);
		$msg['cols'] = $this->ColLength;
		$msg['rows'] = $this->RowLength;
		
		// 中间处理导入逻辑，循环解析excel
		for($rowNum=1;$rowNum<=$msg['rows'];$rowNum++){
			for($colNum=0;$colNum<$msg['cols'];$colNum++){
				$item = $this->PHPExcelImport->getCellByColumnAndRow($colNum,$rowNum);
				$realRow = self::excelValue($item);

				$tmpData = array(
					'title' => $realRow,
					'coordinate' => $item->getCoordinate()
				);
				$excelData['data'][$rowNum][$colNum] = $tmpData;
			}
		}
		
		return array(
			'status' => true,
			'msg' => $excelData
		);
	}
	
	/**
	 * 处理问卷回答的导入
	 * @param type $file	文件名
	 * @param type $type	文件类型 xls or xlsx
	 */
	public function questionAnswerImport($file, $type = 'xlsx'){
		if(!file_exists($file)){
			throw new Exception('文件未找到');
		}
		$this->Type = $type;
		$this->importBase($file);
		$msg['cols'] = $this->ColLength;
		$msg['rows'] = $this->RowLength;
		
		// 中间处理导入逻辑，循环解析excel
		for($rowNum=1;$rowNum<=$msg['rows'];$rowNum++){
			for($colNum=0;$colNum<$msg['cols'];$colNum++){
				$itemType = false;
				$item = $this->PHPExcelImport->getCellByColumnAndRow($colNum,$rowNum);
				$itemType = self::isDataTime($item);
				$realRow = self::excelValue($item);
				
				$tmpData = array(
					'title' => $realRow,
					'coordinate' => $item->getCoordinate()
				);
				$itemType and $tmpData['date_time_status'] = true;
				switch ($rowNum) {
					case 1:	#单个sheet表头信息
						$excelData['data_th'][$rowNum][$colNum] = $tmpData;
						break;
					default:
						$excelData['data'][$rowNum][$colNum] = $tmpData;
						break;
				}
			}
		}
		
		return array(
			'status' => true,
			'msg' => $excelData
		);
		
	}

	
	/**
	 * 处理对标数据回答的导入
	 * @param type $file	文件名
	 * @param type $type	文件类型 xls or xlsx
	 */
	public function questionBenchmarkImport($file, $type = 'xlsx'){
		if(!file_exists($file)){
			throw new Exception('文件未找到');
		}
		$this->Type = $type;
		$this->importBase($file);
		$msg['cols'] = $this->ColLength;
		$msg['rows'] = $this->RowLength;
		
		// 中间处理导入逻辑，循环解析excel
		for($rowNum=1;$rowNum<=$msg['rows'];$rowNum++){
			for($colNum=0;$colNum<$msg['cols'];$colNum++){
				$itemType = false;
				$item = $this->PHPExcelImport->getCellByColumnAndRow($colNum,$rowNum);
				$itemType = self::isDataTime($item);
				$realRow = self::excelValue($item);

				$tmpData = array(
					'title' => $realRow,
					'coordinate' => $item->getCoordinate()
				);
				$itemType and $tmpData['date_time_status'] = true;
				switch ($rowNum) {
					case 1:	#单个sheet表头信息
						$excelData['data_th'][$rowNum][$colNum] = $tmpData;
						break;
					default:
						$excelData['data'][$rowNum][$colNum] = $tmpData;
						break;
				}
			}
		}
		
		return array(
			'status' => true,
			'msg' => $excelData
		);
		
	}
}
